package gold.digger;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC859 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public boolean buddyStrings(String s, String goal) {
            if (s.length() <= 1 || s.length() != goal.length()) return false;
            int diffCount = 0;
            int[] diffPos = new int[3], cnt = new int[26];
            for (int i = 0; i < s.length(); i++) {
                cnt[s.charAt(i) - 'a']++;
                if (s.charAt(i) != goal.charAt(i)) {
                    diffPos[diffCount++] = i;
                }
                if (diffCount > 2) return false;
            }

            if (diffCount == 0) {
                for (int i = 0; i < 26; i++) {
                    if (cnt[i] > 1) return true;
                }

                return false;
            } else if (diffCount == 2) {
                if (s.charAt(diffPos[0]) == goal.charAt(diffPos[1]) && s.charAt(diffPos[1]) == goal.charAt(diffPos[0]))
                    return true;
            }

            return false;
        }
    }

    public void run() {
        Solution solution = new Solution();
        System.out.println(solution.buddyStrings("abcd", "acdb"));
    }

    public static void main(String[] args) throws Exception {
        LC859 an = new LC859();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
